package com.hhkj.api.common.service.biz.impl;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Validator;
import com.hhkj.api.common.common.component.manager.SaleCaptchaManager;
import com.hhkj.api.common.common.cont.SaleCaptchaConst;
import com.hhkj.api.common.common.cont.SaleCaptchaTypeEnum;
import com.hhkj.api.common.common.sms.SaleCaptchaTemplateEnum;
import com.hhkj.api.common.common.sms.model.SaleRegisterSmsTemplateParam;
import com.hhkj.api.common.model.extend.SaleCaptcha;
import com.hhkj.api.common.model.req.SendCaptchaReq;
import com.hhkj.api.common.service.biz.ApiSaleCaptchaBizService;
import com.hhkj.api.sale.common.component.checker.ApiSaleChecker;
import com.hhkj.sms.SmsSender;
import com.hhkj.sms.common.cont.PlatformEnum;
import com.hhkj.sms.common.template.AliDaYuTemplate;
import com.ruoyi.common.exception.GlobalException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * ApiCaptchaBizServiceImpl
 *
 * @author HHKJ-SERVER-ZJ
 * @since 2023/10/25 8:30
 */
@Service
public class ApiSaleCaptchaBizServiceImpl implements ApiSaleCaptchaBizService {

    @Autowired
    private SaleCaptchaManager saleCaptchaManager;
    @Autowired
    private ApiSaleChecker apiSaleChecker;

    @Override
    public void captcha(SendCaptchaReq req) {
        if (!Validator.isMobile(req.getMobile())) {
            throw new GlobalException("手机号格式错误");
        }
        if (SaleCaptchaTypeEnum.REGISTER.getCode().equals(req.getTypeCode())) {
            this.register(req.getMobile());
        }
        if (SaleCaptchaTypeEnum.RESET_PASSWORD.getCode().equals(req.getTypeCode())) {
            this.resetPw(req.getMobile());
        }
        if (SaleCaptchaTypeEnum.UPDATE_PASSWORD.getCode().equals(req.getTypeCode())) {
            this.updatePw(req.getMobile());
        }
        if (SaleCaptchaTypeEnum.REGISTER_STUDENT.getCode().equals(req.getTypeCode())) {
            this.registerStudent(req.getMobile());
        }
    }

    /**
     * 注册学生账号信息
     *
     * @param mobile 手机号
     */
    public void registerStudent(String mobile) {
        // apiSaleChecker.checkerMobileUpdate(mobile);
        // 2、查看缓存中是否有对应的手机号验证码缓存
        SaleCaptcha captcha
                = saleCaptchaManager.getCaptcha(SaleCaptchaTypeEnum.REGISTER_STUDENT.getCode(), mobile);
        // 3、判断是不是超过60s
        if (captcha != null) {
            this.checkCaptchaExpire(captcha);
        }
        // 4、重新生成验证码
        captcha = saleCaptchaManager.createCaptcha(SaleCaptchaTypeEnum.REGISTER_STUDENT.getCode(), mobile);
        AliDaYuTemplate template = SaleCaptchaTemplateEnum.getTemplate(SaleCaptchaTypeEnum.REGISTER_STUDENT.getCode());
        // 5、发送短信
        SmsSender.send(PlatformEnum.ALI_DA_YU, mobile, template.createSmsBody(new SaleRegisterSmsTemplateParam(captcha.getCaptcha())));
    }

    /**
     * 修改密码验证码发送
     *
     * @param mobile 手机号
     */
    public void updatePw(String mobile) {
        apiSaleChecker.checkerMobileUpdate(mobile);
        // 2、查看缓存中是否有对应的手机号验证码缓存
        SaleCaptcha captcha
                = saleCaptchaManager.getCaptcha(SaleCaptchaTypeEnum.UPDATE_PASSWORD.getCode(), mobile);
        // 3、判断是不是超过60s
        if (captcha != null) {
            this.checkCaptchaExpire(captcha);
        }
        // 4、重新生成验证码
        captcha = saleCaptchaManager.createCaptcha(SaleCaptchaTypeEnum.UPDATE_PASSWORD.getCode(), mobile);
        AliDaYuTemplate template = SaleCaptchaTemplateEnum.getTemplate(SaleCaptchaTypeEnum.UPDATE_PASSWORD.getCode());
        // 5、发送短信
        SmsSender.send(PlatformEnum.ALI_DA_YU, mobile, template.createSmsBody(new SaleRegisterSmsTemplateParam(captcha.getCaptcha())));
    }

    /**
     * 重置密码验证码发送
     *
     * @param mobile 手机号
     */
    public void resetPw(String mobile) {
        apiSaleChecker.checkerMobileReset(mobile);

        // 2、查看缓存中是否有对应的手机号验证码缓存
        SaleCaptcha captcha
                = saleCaptchaManager.getCaptcha(SaleCaptchaTypeEnum.RESET_PASSWORD.getCode(), mobile);
        // 3、判断是不是超过60s
        if (captcha != null) {
            this.checkCaptchaExpire(captcha);
        }
        // 4、重新生成验证码
        captcha = saleCaptchaManager.createCaptcha(SaleCaptchaTypeEnum.RESET_PASSWORD.getCode(), mobile);
        AliDaYuTemplate template = SaleCaptchaTemplateEnum.getTemplate(SaleCaptchaTypeEnum.RESET_PASSWORD.getCode());
        // 5、发送短信
        SmsSender.send(PlatformEnum.ALI_DA_YU, mobile, template.createSmsBody(new SaleRegisterSmsTemplateParam(captcha.getCaptcha())));
    }

    /**
     * 校验验证码有效期信息
     *
     * @param captcha 验证码信息
     */
    public void checkCaptchaExpire(SaleCaptcha captcha) {
        long captchaSendingIntervalSecond = DateUtil.between(new Date(), captcha.getCreateTime(), DateUnit.SECOND);
        if (captchaSendingIntervalSecond < SaleCaptchaConst.CAPTCHA_SENDING_INTERVAL_LIMIT_SECOND) {
            throw new GlobalException("验证码已发送，请 " + (SaleCaptchaConst.CAPTCHA_SENDING_INTERVAL_LIMIT_SECOND - captchaSendingIntervalSecond) + "s 后再试");
        }
    }

    /**
     * 获取注册验证码
     *
     * @param mobile 手机号
     */
    private void register(String mobile) {
        // 1、校验手机号是否已注册
        apiSaleChecker.checkerMobileRegister(mobile);
        // 2、查看缓存中是否有对应的手机号验证码缓存
        SaleCaptcha captcha
                = saleCaptchaManager.getCaptcha(SaleCaptchaTypeEnum.REGISTER.getCode(), mobile);
        // 3、判断是不是超过60s
        if (captcha != null) {
            this.checkCaptchaExpire(captcha);
        }
        // 4、重新生成验证码
        captcha = saleCaptchaManager.createCaptcha(SaleCaptchaTypeEnum.REGISTER.getCode(), mobile);
        AliDaYuTemplate template = SaleCaptchaTemplateEnum.getTemplate(SaleCaptchaTypeEnum.REGISTER.getCode());
        // 5、发送短信
        SmsSender.send(PlatformEnum.ALI_DA_YU, mobile, template.createSmsBody(new SaleRegisterSmsTemplateParam(captcha.getCaptcha())));
    }

}
